﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class ProviderDAO
    {
        static DataClasses1DataContext Data = new DataClasses1DataContext();

        public static PROVIDER GetProvider(string UserName, string PassWord)
        {
            try
            {
                PROVIDER provider = Data.PROVIDERs.Single(mem => (mem.ACCOUNT.USERNAME.Trim() == UserName) && (mem.ACCOUNT.PASSWORD.Trim() == PassWord));
                return provider;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static List<PROVIDER> GetAllProvider()
        {
            try
            {
                var query = from provider in Data.PROVIDERs where (provider.ACCOUNT.VISIBLE==true) select provider;
                List<PROVIDER> ListProvider = new List<PROVIDER>();
                foreach (PROVIDER pro in query)
                {
                    ListProvider.Add(pro);
                }
                return ListProvider;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        public static bool Insert(PROVIDER Provider)
        {
            try
            {
                Data.PROVIDERs.InsertOnSubmit(Provider);
                Data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
        }

        public static bool Update(PROVIDER Provider)
        {
            try
            {
                PROVIDER Pro = Data.PROVIDERs.Single(p => p.IDPROVIDER == Provider.IDPROVIDER);
                Pro.ACCOUNT.ADDRESS = Provider.ACCOUNT.ADDRESS;
                Pro.ACCOUNT.PASSWORD = Provider.ACCOUNT.PASSWORD;
                Pro.ACCOUNT.PHONE_NUMBER = Provider.ACCOUNT.PHONE_NUMBER;
                Pro.ACCOUNT.FULLNAME = Provider.ACCOUNT.FULLNAME;
                Pro.INTRODUCE_COMPANY = Provider.INTRODUCE_COMPANY;
                Pro.COMPANY_NAME = Provider.COMPANY_NAME;
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        
        public static PROVIDER GetProvider(int idProvider)
        {
            try
            {
                PROVIDER Provider = Data.PROVIDERs.Single(pro => pro.IDPROVIDER == idProvider);
                return Provider;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public static PROVIDER GetProviderFromAccount(int idAccount)
        {
            try
            {
               bool isExist= Data.PROVIDERs.Any(pr => (pr.IDACCOUNT== idAccount) && (pr.ACCOUNT.VISIBLE == true));
               if (!isExist)
                   return null;
                PROVIDER Provider = Data.PROVIDERs.Single(pro => pro.ACCOUNT.IDACCOUNT == idAccount);
                return Provider;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Delete(int idProvider)
        {
            try
            {
                PROVIDER Provider = Data.PROVIDERs.Single(pro => pro.IDPROVIDER == idProvider);
                Provider.ACCOUNT.VISIBLE = false;
                Data.SubmitChanges();
                CommentDAO.DeleteByAccount(Provider.IDACCOUNT);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool isExisting(int IDProvider)
        {
            return Data.PROVIDERs.Any(pr => (pr.IDPROVIDER == IDProvider)&&(pr.ACCOUNT.VISIBLE==true));
        }

        public static bool isExisting(string User,string Pass)
        {
            return Data.PROVIDERs.Any(pr => (pr.ACCOUNT.USERNAME.Trim() == User)&&(pr.ACCOUNT.PASSWORD.Trim()==Pass) && (pr.ACCOUNT.VISIBLE == true));
        }

        public static bool isExisting(string User)
        {
            return Data.PROVIDERs.Any(pr => (pr.ACCOUNT.USERNAME.Trim() == User) && (pr.ACCOUNT.VISIBLE == true));
        }

        public static int GetIDMax()
        {
            int IDMax = Data.PROVIDERs.Max(pr => pr.IDPROVIDER);
            return IDMax;
        }

        public static PROVIDER GetProvider(string UserName)
        {
            try
            {
                PROVIDER provider = Data.PROVIDERs.Single(mem => (mem.ACCOUNT.USERNAME.Trim() == UserName.Trim())&& (mem.ACCOUNT.VISIBLE==true));
                return provider;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static int GetIDProvider(string companyName)
        {
            try
            {
                PROVIDER provider = Data.PROVIDERs.Single(pro=>pro.COMPANY_NAME.Trim()==companyName.Trim()&&pro.ACCOUNT.VISIBLE==true);
                return provider.IDPROVIDER;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
